<?php 
/**
 * GPS<=>地理位置
 * @Author    紫梦
 * @DateTime  2018-03-01
 * @copyright 响响租车
 * @version   1.0
 */
namespace app\common\controller;
use app\common\request\Curl;

class Gps
{

    public static function geocode($lng,$lat)
    {   
        $host = "http://restapi.amap.com";
        $path = "/v3/geocode/regeo";
        $transgps = self::transgps($lng,$lat);
        if($transgps){
            $trans = json_decode($transgps,true);
            $location = $trans['locations']??'';
            $key = "df2e1065cd36933573cf408a88aaf63e";
            $url = $host . $path . "?" . "key=" . $key . "&location=" . $location . "&batch=false";

            $curl = new Curl; 
            $res = $curl->url($url);

            // 任务结果状态
            if ($curl->error()) {
                $curl->message();
            } else {
                // 任务进程信息
                $info = $curl->info();                
                if($info['http_code'] == 200){
                    // 任务结果内容
                    return $curl->data();

                }
            }           
        }

        return false;
    }

    public static function transgps($lng,$lat){
        $host = "http://restapi.amap.com";
        $path = "/v3/assistant/coordinate/convert";
        $key = "df2e1065cd36933573cf408a88aaf63e";
        $locations = "$lng,$lat&coordsys=gps";
        $url = $host . $path . "?" . "key=" . $key . "&locations=" . $locations;

        $curl = new Curl; 
        // $res = $curl->set('CURLOPT_HTTPHEADER',["Authorization:APPCODE " . $appcode])->url($url);
        $res = $curl->url($url);
        // 任务结果状态
        if ($curl->error()) {
            $curl->message();
        } else {
            // 任务进程信息
            $info = $curl->info();            
            if($info['http_code'] == 200){
                return  $curl->data();
            }
        }
    }

}